//
//  YDYUserAccount.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/20.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYUserAccount.h"
#import "YDYNetWorkTool.h"
//保存用户对象，为了只加载一次
static YDYUserAccount *account;
//保存用户文件的路径
static NSString *filepath ;
@implementation YDYUserAccount
+(void)initialize{
    filepath = @"userAccount.plist".ydy_cachesAddress;
    account = [self loadUserAccount];
}

+(instancetype)sharedAccount{
    return account;
}
-(instancetype)initWithDictonary:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    account = self;
    return  self;
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    return;
}


#pragma mark -
#pragma mark -保存用户
-(BOOL)saveUserAccount{
    
    return [NSKeyedArchiver archiveRootObject:self toFile:filepath];
}
+(instancetype)loadUserAccount{
    if (account != nil)return account;
    account = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
    return account;
}
+(BOOL)isLogin{
    
    if (account == nil)return NO;
    if ([account.expires_date compare:[NSDate dateWithTimeIntervalSinceNow:0]] == NSOrderedAscending)return NO;
    
    return YES;
}


-(void)loadUserInfoFinished:(void(^)(YDYUserAccount *account , NSError *error))finished{
    
    //断言
    NSAssert(self.access_token !=nil, @"使用本方法，必须先获取access_token");
    
    NSString *url = @"2/users/show.json";
    
    NSDictionary *paramas = @{@"access_token":self.access_token,@"uid":self.uid};
    [[YDYNetWorkTool sharedTool]GET:url parameters:paramas progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        self.avatar_large = responseObject[@"avatar_large"];
        self.screen_name = responseObject[@"screen_name"];
        finished(self,nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finished(nil,error);
    }];
}

#pragma mark -
#pragma mark -nscoding

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeInteger:self.expires_in forKey:@"expires_in"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.expires_date forKey:@"expires_date"];
    [aCoder encodeObject:self.avatar_large forKey:@"avatar_large"];
    [aCoder encodeObject:self.screen_name forKey:@"screen_name"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self.access_token  = [aDecoder decodeObjectForKey:@"access_token"];
    self.expires_in = [aDecoder decodeIntegerForKey:@"expires_in"];
    self.uid = [aDecoder decodeObjectForKey:@"uid"];
    self.expires_date = [aDecoder decodeObjectForKey:@"expires_date"];
    self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
    self.screen_name = [aDecoder decodeObjectForKey:@"screen_name"];
    return self;
}

    

#pragma mark -
#pragma mark -懒加载
-(void)setExpires_in:(NSInteger)expires_in{
    _expires_in = expires_in;
    self.expires_date = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)expires_in];
}
@end
